package com.ssish.saas.eccs.util;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.ImageInputStream;

import com.github.jaiimageio.impl.plugins.tiff.TIFFImageReaderSpi;

public class TiffUtil {
    
    /**
     * tif文件处理工具类
     * 可以将tif文件转换成多张jpg 或png 图片
     * @param tiffFileInput
     * @return
     */
    public static List<InputStream> getJpgFromTiff(InputStream tiffFileInput){
        
        List<InputStream> list = new ArrayList<InputStream>();
        int pages=0;
                BufferedImage[] bi=null;
                ImageReader tiffReader = null;
                ImageInputStream input = null;
                try {
                    input = ImageIO.createImageInputStream(tiffFileInput);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                     e1.printStackTrace();
                }
                IIORegistry iioreg = IIORegistry.getDefaultInstance();
                iioreg.registerApplicationClasspathSpis();
                ImageReaderSpi irs = new TIFFImageReaderSpi();
                try {
                    tiffReader = irs.createReaderInstance();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                tiffReader.setInput(input);
                
                try {
                    pages = tiffReader.getNumImages(true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                if(pages>0){
                    bi=new BufferedImage[pages];
                    for (int i = 0; i < pages; i++) {
                        System.out.println(i);
                        try {
                            bi[i]=tiffReader.read(i);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
//                        Image image = bi[i].getScaledInstance(bi[i].getWidth(), bi[i].getHeight(), Image.SCALE_DEFAULT);
                        BufferedImage tag = new BufferedImage(bi[i].getWidth(), bi[i].getHeight(), BufferedImage.TYPE_INT_BGR);
//                        Graphics g = tag.getGraphics();
//                        g.drawImage(image, 0, 0, null);
//                        g.dispose();
                        list.add(bufferedImageToInputStream(tag));
                    }
                }
        return list;
    }
    
    
    public static void main(String[] args) {
        String tifPath="D:/test.tif";
        int pages=0;
                BufferedImage[] bi=null;
                File tiffFile = new File(tifPath);
                ImageReader tiffReader = null;
                ImageInputStream input = null;
                try {
                    input = new FileImageInputStream(tiffFile);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                IIORegistry iioreg = IIORegistry.getDefaultInstance();
                iioreg.registerApplicationClasspathSpis();
                ImageReaderSpi irs = new TIFFImageReaderSpi();
                try {
                    tiffReader = irs.createReaderInstance();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                tiffReader.setInput(input);
                
                try {
                    pages = tiffReader.getNumImages(true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                if(pages>0){
                    bi=new BufferedImage[pages];
                    for (int i = 0; i < pages; i++) {
                        System.out.println(i);
                        try {
                            bi[i]=tiffReader.read(i);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        Image image = bi[i].getScaledInstance(bi[i].getWidth(), bi[i].getHeight(), Image.SCALE_DEFAULT);
                        BufferedImage tag = new BufferedImage(bi[i].getWidth(), bi[i].getHeight(), BufferedImage.TYPE_INT_BGR);
                        Graphics g = tag.getGraphics();
                        g.drawImage(image, 0, 0, null);
                        g.dispose();
                        try {
//                            ImageIO.write(tag, "GIF", new File("D:/"+i+"11.gif"));
                            ImageIO.write(tag, "JPG", new File("D:/"+i+"11.JPG"));
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

    }
    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public static InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
